﻿
using Qiniu.Http;
using System;
using Qiniu.Util;
using qiniu_class;

namespace Qiniu.Storage
{
    public class FormUploaderEx : QiniuBase
    {
        private PutExtra extra = new PutExtra();

        public event UploadProgressHandler OnUploadProgress
        {
            add { this.extra.ProgressHandler += value; }
            remove { this.extra.ProgressHandler -= value; }
        }
        public void UploadFile(string pLoaclFilenName, string pFilenName, string callbackUrl, string pAppendCallBackBody)
        {
            try
            {
                Mac mac = new Mac(AccessKey, SecretKey);
                Random rand = new Random();
                //string key = string.Format("UploadFileTest_{0}.dat", rand.Next());
                string key = string.Format(pFilenName);
                //string filePath = LocalFile;

                PutPolicy putPolicy = new PutPolicy();
                putPolicy.Scope = Bucket + ":" + key;
                putPolicy.SetExpires(36000);
                putPolicy.DeleteAfterDays = 1;
                if (callbackUrl != null)
                {
                    string AllowCallBack = System.Configuration.ConfigurationManager.AppSettings["AllowCallBack"]?.ToString();
                    if (AllowCallBack?.ToLower() == "true")
                    {
                        putPolicy.CallbackUrl = callbackUrl;
                        Console.WriteLine("callbackurl : {0}", callbackUrl);
                    }

                }
                putPolicy.CallbackBody = "name=$(fname)& hash =$(etag)" + pAppendCallBackBody ?? string.Empty;
                string token = Auth.CreateUploadToken(mac, putPolicy.ToJsonString());
                Config config = new Config();
                config.Zone = Zone.ZONE_CN_East;
                config.UseHttps = true;
                config.UseCdnDomains = true;
                config.ChunkSize = ChunkUnit.U512K;

                ResumableUploader target = new ResumableUploader(config);
                HttpResult result = target.UploadFile(pLoaclFilenName, key, token, extra);
                Console.WriteLine("form upload result: " + result.ToString());
                //Assert.AreEqual((int)HttpCode.OK, result.Code);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }

        public void UploadFileDD(String pLoaclFilenName, String pFilenName)
        {
            try
            {
                Mac mac = new Mac(AccessKey, SecretKey);
                Random rand = new Random();
                //string key = string.Format("UploadFileTest_{0}.dat", rand.Next());
                string key = string.Format(pFilenName);
                //string filePath = LocalFile;

                PutPolicy putPolicy = new PutPolicy();
                putPolicy.Scope = Bucket + ":" + key;
                putPolicy.SetExpires(36000);
                putPolicy.DeleteAfterDays = 1;
                string token = Auth.CreateUploadToken(mac, putPolicy.ToJsonString());
                Config config = new Config();
                config.Zone = Zone.ZONE_CN_East;
                config.UseHttps = true;
                config.UseCdnDomains = true;
                config.ChunkSize = ChunkUnit.U1024K;
                ResumableUploader target = new ResumableUploader(config);
//                FormUploader target = new FormUploader(config);
                HttpResult result = target.UploadFile(pLoaclFilenName, key, token, extra);
                Console.WriteLine("form upload result: " + result.ToString());
                //Assert.AreEqual((int)HttpCode.OK, result.Code);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
    }
}